package com.wxit.algorithm.heap;

/**
 * 数据流中的第 K 大元素
 *
 *
 */
public class LeetCode703 {

    private MinHeap head;


    public LeetCode703(int k, int[] nums) {
        head = new MinHeap(k);
        for (int num : nums) {
            add(num);
        }
    }

    public int add(int val) {
        if (!head.isFull()) {
            head.offer(val);
        } else if (val > head.peek()) {
            head.replace(val);
        }
        return head.peek();
    }

    public static void main(String[] args) {
        LeetCode703 test = new LeetCode703(3, new int[]{4, 5, 8, 2});

        System.out.println(test.add(3));
        System.out.println(test.add(5));
        System.out.println(test.add(10));
    }
}
